﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using BUS;

namespace GUI
{
    public partial class frmQuanLyNhanVien : DevComponents.DotNetBar.Office2007Form
    {
        public frmQuanLyNhanVien()
        {
            InitializeComponent();
        }

        private void frmQuanLyNhanVien_Load(object sender, EventArgs e)
        {
            LayDS_Loai();
            LayDS_NV();
            lbNgay.Text = DateTime.Now.ToShortDateString();
        }
        public void LayDS_Loai()
        {
            NhanVienBUS busNV = new NhanVienBUS();
            List<LOAINHANVIEN> arrLoai = busNV.LayDS_Loai();
            cbbLoaiNV.DataSource = arrLoai;
            cbbLoaiNV.DisplayMember = "TenLoaiNV";
            cbbLoaiNV.ValueMember = "MaLoaiNV";
        }
        public void LayDS_NV()
        {
            gvNhanVien.Rows.Clear();
            NhanVienBUS busNV = new NhanVienBUS();
            List<NHANVIEN> arrNhanVien = busNV.LayDS_NV();
            for (int i = 0; i < arrNhanVien.Count; i++)
            {
                DataGridViewRow _newRow = (DataGridViewRow)gvNhanVien.Rows[0].Clone();
                _newRow.Cells[0].Value = arrNhanVien[i].MaNV;
                _newRow.Cells[1].Value = arrNhanVien[i].TenNV;
                _newRow.Cells[2].Value = arrNhanVien[i].CMND;
                _newRow.Cells[3].Value = arrNhanVien[i].DienThoai;
                _newRow.Cells[4].Value = arrNhanVien[i].DiaChi;
                _newRow.Cells[5].Value = arrNhanVien[i].LOAINHANVIEN.MaLoaiNV;
                _newRow.Cells[6].Value = arrNhanVien[i].LOAINHANVIEN.TenLoaiNV;
                gvNhanVien.Rows.Add(_newRow);
                
            }
        }
        private void gbThemNV_Enter(object sender, EventArgs e)
        {

        }

        private void txtCMND_TextChanged(object sender, EventArgs e)
        {

        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }
        public void clear()
        {
            txtTenNV.Text = "";
            txtCMND.Text = "";
            txtDiaChi.Text = "";
            txtSDT.Text = "";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (kiemtra())
            {
                NHANVIEN nv = new NHANVIEN();
                nv.MaNV = MaNV_Sua;
                nv.TenNV = txtTenNV.Text;
                nv.CMND = txtCMND.Text;
                nv.DiaChi = txtDiaChi.Text;
                nv.DienThoai = txtSDT.Text;
                nv.LoaiNV = int.Parse(cbbLoaiNV.SelectedValue.ToString());

                //NhanVienBUS bus = new NhanVienBUS();
               NhanVienBUS busNV = new NhanVienBUS();

                busNV.CapNhat_NV(nv);
                clear();
                LayDS_NV();
                pnThemNhanVien.Visible = false;
            }
           
        }
        int MaNV_Sua;
        private void gvNhanVien_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            if (rowIndex >= 0 && rowIndex < gvNhanVien.RowCount - 1)
            {
                pnThemNhanVien.Visible = true;
                btnCapNhatNV.Visible = true;
                btnThemNV.Visible = false;
                btnThem.Enabled = true;

                DataGridViewRow row = gvNhanVien.Rows[rowIndex];
                MaNV_Sua = int.Parse(row.Cells["clMaNV"].Value.ToString());
                txtTenNV.Text = row.Cells["clTenNV"].Value.ToString();
                txtCMND.Text = row.Cells["clCMND"].Value.ToString();
                txtDiaChi.Text = row.Cells["clDiaChi"].Value.ToString();
                txtSDT.Text = row.Cells["clDT"].Value.ToString();
                cbbLoaiNV.SelectedValue = int.Parse(row.Cells["clMaLoai"].Value.ToString());
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            clear();
            pnThemNhanVien.Visible = true;
            btnThemNV.Visible = true;
            btnCapNhatNV.Visible = false;
            btnThem.Enabled = false;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            clear();
            pnThemNhanVien.Visible = false;
            btnThem.Enabled = true;
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            clear();
            pnThemNhanVien.Visible = false;
            btnThem.Enabled = true;
        }
        private bool kiemtra()
        {
            if (string.IsNullOrEmpty(txtTenNV.Text))
            {
                MessageBox.Show("Nhập Tên Nhân Viên!");
                return false;
            }
            if (string.IsNullOrEmpty(txtCMND.Text))
            {
                MessageBox.Show("Nhập Số CNMD!");
                return false;
            }
            return true;
        }
        private void btnThemNV_Click(object sender, EventArgs e)
        {
            if (kiemtra())
            {
                NHANVIEN nv = new NHANVIEN();
                nv.MaNV = 0;
                nv.TenNV = txtTenNV.Text;
                nv.CMND = txtCMND.Text;
                nv.MatKhau = md5.maHoaMd5(txtCMND.Text);

                nv.DiaChi = txtDiaChi.Text;
                nv.DienThoai = txtSDT.Text;
                nv.LoaiNV = int.Parse(cbbLoaiNV.SelectedValue.ToString());
                nv.TinhTrang = true;


                NhanVienBUS busNV = new NhanVienBUS();

                busNV.Them_NV(nv);
                clear();
                LayDS_NV();
            }
           
           
        }

      
    }
}
